﻿/// <reference path="knockout-2.2.1.js" />
/// <reference path="server.js" />

var menu = (function() {
    // contains class(classes) describing single menu item

    var MenuItem = function(object) {
        this.name = object.name;
        this.id = object.id;
        this.price = object.price;
        this.time = object.price;
    };
    //Generates menu item and makes sure that its data is valid

    function generateMenuItem(id, name, price, time) {
        if (isNaN(id) || typeof id === 'undefined') {
            throw new TypeError("Item ID is not a number");
        } else if (id < 0) {
            throw new RangeError("Item ID cannot be negative");
        }

        if (name === "" || typeof name === 'undefined') {
            throw new TypeError("Item name is empty string or undefiend");
        }

        if (isNaN(price) || typeof price === 'undefined') {
            throw new TypeError("Price is not a valid number");
        } else if (price < 0) {
            throw new RangeError("Price cannot be negative");
        }

        if (isNaN(time) || typeof time === 'undefined') {
            throw new TypeError("Preparation time is not a valid number");
        } else if (time <= 0) {
            throw new RangeError("Preparation time cannot be negative or zero");
        }

        var jsonObject = {
            "itemID": id,
            "name": name,
            "price": price,
            "preparationTime": time
        };
        return jsonObject;
    }
    //Saves menu item. Has to be finished

    function storeMenuItem(item) {
        try {
            validateMenuItem(item);
            itemsArray = server.loadData(item);
            itemsArray.push(item);
            server.saveData(server.nameConstants.menuItems, itemsArray);
        } catch (e) {
            console.log(e.name + ": " + e.message);
        }
    }

    return {
        MenuItem: MenuItem,
        generateMenuItem: generateMenuItem
    };
})();